package com.xzk.controller;

import com.xzk.bean.Message;
import com.xzk.exception.DuplicateNumberException;
import com.xzk.service.AdminService;
import com.xzk.util.UserUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @ClassName AdminController
 * @Description 管理员控制器
 * @Author 0715-YuHao
 * @Date 2020/9/28 16:53
 * @Version 1.0
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Resource(name = "adminService")
    private AdminService service;

    @RequestMapping(method = RequestMethod.POST, value = "/login.do")
    public @ResponseBody Message login(String username, String password,
                                       HttpServletRequest request, HttpServletResponse response) {
        ServletContext application = request.getServletContext();
        Map<String, String> loginMap = (Map<String, String>) application.getAttribute("loginMap");
        if (loginMap == null) {
            loginMap = new HashMap<>();
        }
        Map<String, String> map = new HashMap<>();
        map.put("username", username);
        map.put("password", password);
        boolean login = service.login(map);
        Message msg;
        if (login) {
            msg = new Message(0, "登录成功！");
            Map<String, String> map1 = new HashMap<>();
            map1.put("ip", request.getRemoteAddr());
            map1.put("username", username);
            service.updateLoginTime(map1);
            String timeStamp = String.valueOf(new Date().getTime());
            loginMap.put("admin" + username, timeStamp);
            application.setAttribute("loginMap", loginMap);
            Cookie cookie = new Cookie("admin" + username, timeStamp);
            cookie.setMaxAge(7 * 24 * 60 * 60);
            response.addCookie(cookie);
            request.getSession().setAttribute("adminUsername", "admin" + username);
        }else {
            msg = new Message(-1, "登录失败,用户名或密码错误！");
        }
        return msg;
    }

    @RequestMapping("/login2.do")
    public @ResponseBody Message login2(HttpServletRequest request, HttpSession session) {
        ServletContext application = request.getServletContext();
        Map<String, String> loginMap = (Map<String, String>) application.getAttribute("loginMap");
        if (loginMap == null) {
            return new Message(-1, "未登录！");
        }
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if ("admin".equals(cookie.getName().substring(0,5))) {
                if (loginMap.get(cookie.getName()).equals(cookie.getValue())) {
                    session.setAttribute("adminUsername", cookie.getName());
                    return new Message(0, "登录成功！");
                }
            }
        }
        return new Message(-1, "未登录！");
    }

    @RequestMapping("/logout.do")
    public String logout(HttpServletRequest request, HttpServletResponse response,HttpSession session) {
        ServletContext application = request.getServletContext();
        Map<String, String> loginMap = (Map<String, String>) application.getAttribute("loginMap");
        String username = UserUtil.getAdminUserName(session);
        session.invalidate();
        loginMap.remove(username);
        application.setAttribute("loginMap", loginMap);
        Cookie cookie = new Cookie(username, "cancel");
        cookie.setMaxAge(0);
        response.addCookie(cookie);
        return "redirect:/admin/login.html";
    }

    @RequestMapping("/getUsername.do")
    public @ResponseBody Message getUsername(HttpSession session) {
        String username = UserUtil.getAdminUserName(session);
        return new Message(0, username.substring(5));
    }

    @ExceptionHandler(DuplicateNumberException.class)
    public @ResponseBody Message NumberError() {
        return new Message(-1, "单号已存在！");
    }
}
